#include "wap_checkbox.h"
#include "wa_wm.h"
#include "wa_win.h"
#include "wa_screen.h"
#include "wa_checkbox.h"
#include "wa_label.h"
#include "wap.h"


static void wap_checkbox_create(void);
static void wap_checkbox_del(void);
static void wap_checkbox_callback(wa_sign* sign);

wa_screen_obj wap_checkbox_screen = 
{
	.id = WAP_WIN_CHECKBOX_ID,
	.create = wap_checkbox_create,
	.del = wap_checkbox_del,
};

wa_obj* wap_checkbox_win;
wa_obj* wap_checkbox_checkbox_up;
wa_obj* wap_checkbox_checkbox_down;
wa_obj* wap_checkbox_checkbox_left;
wa_obj* wap_checkbox_checkbox_right;
wa_obj* wap_checkbox_label_up;
wa_obj* wap_checkbox_label_down;
wa_obj* wap_checkbox_label_left;
wa_obj* wap_checkbox_label_right;

extern wa_font font_song_16_ascii;

wa_result wap_checkbox_up_state;
wa_result wap_checkbox_down_state;
wa_result wap_checkbox_left_state;
wa_result wap_checkbox_right_state;

void wap_checkbox_init(void)
{
	wap_checkbox_up_state = wa_nok;
	wap_checkbox_down_state = wa_nok;
	wap_checkbox_left_state = wa_nok;
	wap_checkbox_right_state = wa_nok;
}

static void wap_checkbox_create(void)
{
	wap_checkbox_win = wa_win_create(NULL);
	wa_win_set_pos(wap_checkbox_win, 0, 0);
	wa_win_set_size(wap_checkbox_win, WA_LCD_W, WA_LCD_H);
	wa_win_set_edge_show(wap_checkbox_win, wa_nok);
	wa_win_set_callback(wap_checkbox_win, wap_checkbox_callback);
	wap_checkbox_screen.win = wap_checkbox_win;
	
	wap_checkbox_label_up = wa_label_create(NULL);
	wa_win_add_obj(wap_checkbox_win, wap_checkbox_label_up);
	wa_obj_set_pos(wap_checkbox_label_up, 8, 0);
	wa_obj_set_size(wap_checkbox_label_up, WA_LCD_W, WA_LCD_H);
	wa_label_set_font(wap_checkbox_label_up, &font_song_16_ascii);
	wa_label_set_text(wap_checkbox_label_up, "UP");
	
	wap_checkbox_label_down = wa_label_create(NULL);
	wa_win_add_obj(wap_checkbox_win, wap_checkbox_label_down);
	wa_obj_set_pos(wap_checkbox_label_down, 8, 16);
	wa_obj_set_size(wap_checkbox_label_down, WA_LCD_W, WA_LCD_H);
	wa_label_set_font(wap_checkbox_label_down, &font_song_16_ascii);
	wa_label_set_text(wap_checkbox_label_down, "DOWN");
	
	wap_checkbox_label_left = wa_label_create(NULL);
	wa_win_add_obj(wap_checkbox_win, wap_checkbox_label_left);
	wa_obj_set_pos(wap_checkbox_label_left, 8, 32);
	wa_obj_set_size(wap_checkbox_label_left, WA_LCD_W, WA_LCD_H);
	wa_label_set_font(wap_checkbox_label_left, &font_song_16_ascii);
	wa_label_set_text(wap_checkbox_label_left, "LEFT");
	
	wap_checkbox_label_right = wa_label_create(NULL);
	wa_win_add_obj(wap_checkbox_win, wap_checkbox_label_right);
	wa_obj_set_pos(wap_checkbox_label_right, 8, 48);
	wa_obj_set_size(wap_checkbox_label_right, WA_LCD_W, WA_LCD_H);
	wa_label_set_font(wap_checkbox_label_right, &font_song_16_ascii);
	wa_label_set_text(wap_checkbox_label_right, "RIGHT");
	
	wap_checkbox_checkbox_up = wa_checkbox_create(NULL);
	wa_win_add_obj(wap_checkbox_win, wap_checkbox_checkbox_up);
	wa_obj_set_pos(wap_checkbox_checkbox_up, 64, 4);
	wa_obj_set_size(wap_checkbox_checkbox_up, 8, 8);
	wa_checkbox_set_back_color(wap_checkbox_checkbox_up, WA_COLOR_BACKGROUND);
	wa_checkbox_set_fore_color(wap_checkbox_checkbox_up, WA_COLOR_FOREGROUND);
	wa_checkbox_set_edge_size(wap_checkbox_checkbox_up, 1);
	wa_checkbox_set_interval(wap_checkbox_checkbox_up, 1);
	wa_checkbox_set_state(wap_checkbox_checkbox_up, wap_checkbox_up_state);
	
	wap_checkbox_checkbox_down = wa_checkbox_create(NULL);
	wa_win_add_obj(wap_checkbox_win, wap_checkbox_checkbox_down);
	wa_obj_set_pos(wap_checkbox_checkbox_down, 64, 20);
	wa_obj_set_size(wap_checkbox_checkbox_down, 8, 8);
	wa_checkbox_set_back_color(wap_checkbox_checkbox_down, WA_COLOR_BACKGROUND);
	wa_checkbox_set_fore_color(wap_checkbox_checkbox_down, WA_COLOR_FOREGROUND);
	wa_checkbox_set_edge_size(wap_checkbox_checkbox_down, 1);
	wa_checkbox_set_interval(wap_checkbox_checkbox_down, 1);
	wa_checkbox_set_state(wap_checkbox_checkbox_down, wap_checkbox_down_state);
	
	wap_checkbox_checkbox_left = wa_checkbox_create(NULL);
	wa_win_add_obj(wap_checkbox_win, wap_checkbox_checkbox_left);
	wa_obj_set_pos(wap_checkbox_checkbox_left, 64, 36);
	wa_obj_set_size(wap_checkbox_checkbox_left, 8, 8);
	wa_checkbox_set_back_color(wap_checkbox_checkbox_left, WA_COLOR_BACKGROUND);
	wa_checkbox_set_fore_color(wap_checkbox_checkbox_left, WA_COLOR_FOREGROUND);
	wa_checkbox_set_edge_size(wap_checkbox_checkbox_left, 1);
	wa_checkbox_set_interval(wap_checkbox_checkbox_left, 1);
	wa_checkbox_set_state(wap_checkbox_checkbox_left, wap_checkbox_left_state);
	
	wap_checkbox_checkbox_right = wa_checkbox_create(NULL);
	wa_win_add_obj(wap_checkbox_win, wap_checkbox_checkbox_right);
	wa_obj_set_pos(wap_checkbox_checkbox_right, 64, 52);
	wa_obj_set_size(wap_checkbox_checkbox_right, 8, 8);
	wa_checkbox_set_back_color(wap_checkbox_checkbox_right, WA_COLOR_BACKGROUND);
	wa_checkbox_set_fore_color(wap_checkbox_checkbox_right, WA_COLOR_FOREGROUND);
	wa_checkbox_set_edge_size(wap_checkbox_checkbox_right, 1);
	wa_checkbox_set_interval(wap_checkbox_checkbox_right, 1);
	wa_checkbox_set_state(wap_checkbox_checkbox_right, wap_checkbox_right_state);
}

static void wap_checkbox_del(void)
{
	wa_obj_del(wap_checkbox_win);
}

static void _wap_checkbox_paint(wa_obj* obj)
{
}

static void wap_checkbox_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_wap_checkbox_paint(obj);
			break;
		}
		case WA_SIGNAL_NOTIFY_PARENT:
		{	
			break;
		}
		case WA_SIGNAL_USER_KEY_UP:
		{	
			if(wap_checkbox_up_state == wa_ok)
			{
				wap_checkbox_up_state = wa_nok;
			}
			else
			{
				wap_checkbox_up_state = wa_ok;
			}
			wa_checkbox_set_state(wap_checkbox_checkbox_up, wap_checkbox_up_state);
			break;
		}
		case WA_SIGNAL_USER_KEY_DOWN:
		{	
			if(wap_checkbox_down_state == wa_ok)
			{
				wap_checkbox_down_state = wa_nok;
			}
			else
			{
				wap_checkbox_down_state = wa_ok;
			}
			wa_checkbox_set_state(wap_checkbox_checkbox_down, wap_checkbox_down_state);
			break;
		}
		case WA_SIGNAL_USER_KEY_LEFT:
		{	
			if(wap_checkbox_left_state == wa_ok)
			{
				wap_checkbox_left_state = wa_nok;
			}
			else
			{
				wap_checkbox_left_state = wa_ok;
			}
			wa_checkbox_set_state(wap_checkbox_checkbox_left, wap_checkbox_left_state);
			break;
		}
		case WA_SIGNAL_USER_KEY_RIGHT:
		{	
			if(wap_checkbox_right_state == wa_ok)
			{
				wap_checkbox_right_state = wa_nok;
			}
			else
			{
				wap_checkbox_right_state = wa_ok;
			}
			wa_checkbox_set_state(wap_checkbox_checkbox_right, wap_checkbox_right_state);
			break;
		}
		case WA_SIGNAL_USER_KEY_ESC:
		{	
			wa_screen_switch(WAP_WIN_MENU_ID, WA_SCREEN_SWITCH_TYPE_LEFT_TOGETHER, 500, wa_nok, wa_nok);
			break;
		}
		default:
		{
			break;
		}
	}
}
